package drive.Mapper;

import drive.Model.FavoriteRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteRecordMapper {
    // 查询所有收藏记录
    @Select("SELECT * FROM favoriterecord")
    List<FavoriteRecord> findAll();

    // 根据favoriteID查询收藏记录
    @Select("SELECT * FROM favoriterecord WHERE favoriteID = #{favoriteID}")
    FavoriteRecord findByFavoriteID(int favoriteID);

    // 根据userID查询收藏记录
    @Select("SELECT * FROM favoriterecord WHERE userID = #{userID}")
    List<FavoriteRecord> findByUserID(int userID);

    // 新增收藏记录
    @Insert("INSERT INTO favoriterecord (userID, contentID, creationTime, contentType) VALUES (#{userID}, #{contentID}, #{creationTime}, #{contentType})")
    int add(FavoriteRecord favoriteRecord);

    // 更新收藏记录
    @Update("UPDATE favoriterecord SET userID = #{userID}, contentID = #{contentID}, creationTime = #{creationTime}, contentType = #{contentType} WHERE favoriteID = #{favoriteID}")
    int update(FavoriteRecord favoriteRecord);

    // 删除收藏记录
    @Delete("DELETE FROM favoriterecord WHERE favoriteID = #{favoriteID}")
    int delete(int favoriteID);

    @Select("SELECT * FROM favoriterecord Where userID = #{userID} AND contentType = 'article' ORDER BY creationTime DESC LIMIT #{PageInit}, #{PageSize}")
    List<FavoriteRecord> findPaginated(int userID,int PageInit,int PageSize);
    @Select("SELECT * FROM favoriterecord Where userID = #{userID} AND contentType = 'item' ORDER BY creationTime DESC LIMIT #{PageInit}, #{PageSize}")
    List<FavoriteRecord> findPageItem(int userID,int PageInit,int PageSize);
    /**
     * 根据文章ID统计收藏数量
     * @param articleId 文章ID
     * @return 收藏数量
     */
    @Select("SELECT COUNT(*) FROM favoriterecord WHERE contentID = #{articleId} AND contentType = 'article'")
    int countByArticleId(int articleId);
    /**
     * 根据用户ID和商品ID查找收藏记录
     * @param userId 用户ID
     * @param itemId 商品ID
     * @return 收藏记录对象
     */
    @Select("SELECT * FROM favoriterecord WHERE userID = #{userId} AND contentID = #{itemId} AND contentType = 'item'")
    FavoriteRecord findByUserIDAndItemID(int userId, int itemId);

    /**
     * 根据用户ID和文章ID查找收藏记录
     * @param userID 用户ID
     * @param articleID 文章ID
     * @return 收藏记录对象
     */
    @Select("SELECT * FROM favoriterecord WHERE userID = #{userID} AND contentID = #{articleID} AND contentType = 'article'")
    FavoriteRecord findByUserAndArticle(int userID, int articleID);
}
